//
//    Tokaplot
//    https://code.google.com/p/tokaplot/
//
//    License: GPL v3
//    https://code.google.com/p/tokaplot/wiki/License
//
//    This file is part of Tokaplot.
//
//    Tokaplot is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Tokaplot is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Tokaplot.  If not, see <http://www.gnu.org/licenses/>
//

package com.tokaplot;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.regex.Pattern;

//import android.util.Log;

// Class for reading memory information
public class ReadMemory {
	
	//private static final String TAG = "Tokaplot";
		
	boolean fileExists = false;
	
	private static Pattern pMemTotal = Pattern.compile("MemTotal.*");
	private static Pattern pMemFree = Pattern.compile("MemFree.*");
	private static Pattern pMemCached = Pattern.compile("Cached.*");
	
	private static Pattern pSpaceSplit = Pattern.compile("\\s+");
	
	public ReadMemory() {
		
		// Try and open file
		File fMem = new File("//proc","meminfo");
    	if (fMem.exists() == false) {
    		fileExists = false;
    	} else {
    		fileExists = true;
    	}
	}
	
	public boolean canRead() {
    	return fileExists;
	}
	
	public boolean read(float [] mem_val) {
		
		File fMem = new File("//proc","meminfo");
    	if (fMem != null && fMem.exists()) {
			try {
				
			    String line;
			    String [] lineElements;
			    int nMemTotal = 0;
			    int nMemFree = 0;
			    int nMemCached = 0;
			    
			    //Read file
			    BufferedReader brMem = new BufferedReader(new FileReader(fMem), 1000);
			    while((line = brMem.readLine()) != null) {
			    	
			    	// Break up line
				    line=line.trim();
			    	
			    	// Parse line parts
			    	if (pMemTotal.matcher(line).matches()) {
			    		lineElements = pSpaceSplit.split(line, 17);
			    		nMemTotal = Integer.parseInt(lineElements[1]);
			    	} else if (pMemFree.matcher(line).matches()) {
			    		lineElements = pSpaceSplit.split(line, 17);
			    		nMemFree = Integer.parseInt(lineElements[1]);
			    	} else if (pMemCached.matcher(line).matches()) {
			    		lineElements = pSpaceSplit.split(line, 17);
			    		nMemCached = Integer.parseInt(lineElements[1]);
			    	}			    	
			    	
			    	// Don't read any more if we have info we need
			    	if (nMemTotal>0 && nMemFree>0 && nMemCached>0)
			    		break;
			    }
			    brMem.close();
			    
			    if (nMemTotal>0 && nMemFree>0 && nMemCached>0) {
			    	
			    	// Set memory value
			    	mem_val[0] = 100.0f * (float)(nMemFree + nMemCached)/(float)nMemTotal;
				    return true;
			    }
			    
			    // Can't find memory values
			    return false;
			}
			catch (Exception e) {
				
				// Can't read file
				return false;
			}
    	}
    	return false;
	}
	
}
